<html>
<script>

function chunk(array, process, context){
    var items = array.concat();   //clone the array
    setTimeout(function(){
        var item = items.shift();
        process.call(context, item);
 
        if(items.length > 0){

            setTimeout(arguments.callee, 100); // points to setTimeout
            // setTimeout(chunk, 100); // Uncaught TypeError: Cannot read property 'concat' of undefined
        }
    }, 100);
}

/*console.time("measure");
console.log("start");
chunk([1,2,3,4,5,6], function(oItem) {
	console.log(this.name + ", " + oItem);
}, { name: "Jerry"});
// 看起来像是异步执行的
console.timeEnd("measure");
console.log("end");*/

function timedChunk(items, process, context, callback){
    var todo = items.concat();   //create a clone of the original
 
    setTimeout(function(){
 
        var start = + new Date();
 
        do {
             process.call(context, todo.shift());
        } while(todo.length > 0 && (+ new Date() - start < 50));
 
        if(todo.length > 0){
            setTimeout(arguments.callee, 25);
        } else{
            callback(items);
        }
    }, 25);
}

timedChunk([1,2,3,4,5,6], function(oItem) {
	console.log(this.name + ", " + oItem);
}, { name: "Jerry"}, function() { console.log("over!");});


</script>
</html>